<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="所属公司">
        <el-select v-model="formInline.gongsiSel" @change="initData" placeholder="所属公司">
          <el-option v-for="val in gongsiList" :key="val.id" :label="val.name" :value="val.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="openAddPage">
      新增
    </el-button>

    <el-table :data="dataList" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" prop="name" label="分组名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          {{ timeChange(scope.row.created) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">
            编辑
          </el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'修改分组':'新增分组'">
      <el-form :model="fromData" :rules="rules" ref="ruleForm" label-width="160px" label-position="left">
        <el-form-item label="分组名称" prop="name">
          <el-input v-model="fromData.name" placeholder="请输入分组名称" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">
          取消
        </el-button>
        <el-button type="primary" @click="confirm('ruleForm')">
          确定
        </el-button>
      </div>
    </el-dialog>
    <!-- <Pagination :total="total" :page="page" @pagination="pagination" /> -->
  </div>
</template>

<script>
import { deepClone,formatTime } from '@/utils'
import { putDeviceGroup, deleteDeviceGroup, postDeviceGroup, getDeviceGroup } from '@/api/DeviceGroup'
import Pagination from '@/components/Pagination/index'

const defaultRole = {
  id: '',
  name: '',
  created:''
}

export default {
  data() {
    return {
      fromData: Object.assign({}, defaultRole),
      dataList: [],
      dialogVisible: false,
      dialogType: 'new',
      rules: {
        name: [
          { required: true, message: '请输入分组名称' },
        ],
      },
      total: 20,
      page: 1,
      formInline: {
        name: '',
        gongsiSel: ''
      }
    }
  },
  computed: {
  },
  components: { Pagination },
  created() {
    this.gongsiList = JSON.parse(localStorage.getItem('companyList'));
    this.formInline.gongsiSel = this.gongsiList[0].id;
    this.initData();
  },
  methods: {
    //列表
    initData(){
      getDeviceGroup(this.formInline.gongsiSel).then((res)=>{
        console.log(res)
        this.dataList = res.result;
      })
    },
    //过滤查询
    check(){
      this.initData();
    },
    //时间格式转化
    timeChange(UTCDateString) {
      if (!UTCDateString) {
        return '-';
      }
      function formatFunc(str) {
        return str > 9 ? str : '0' + str
      }
      var date2 = new Date(UTCDateString);
      console.log('时间', date2)
      var year = date2.getFullYear();
      var mon = formatFunc(date2.getMonth() + 1);
      var day = formatFunc(date2.getDate());
      var hour = date2.getHours();
      var noon = hour >= 12 ? 'PM' : 'AM'; // 判断是上午还是下午
      hour = hour >= 12 ? hour - 12 : hour; // 12小时制
      hour = formatFunc(hour);
      var min = formatFunc(date2.getMinutes());
      var sec = formatFunc(date2.getSeconds());
      var dateStr = year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + sec;
      return dateStr;
    },
    //分页
    pagination(data) {
      console.log(data)
    },
    openAddPage(){
      this.dialogType = 'new'
      this.dialogVisible = true;
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.fromData = deepClone(scope.row)
    },
    handleDelete({ $index, row }) {
      this.$confirm('确认删除?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          deleteDeviceGroup(row.id).then((res)=>{
            this.initData();
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        })
        .catch(err => { console.error(err) })
    },
    async confirm(formName) {
      if(this.dialogType == 'edit'){
        putDeviceGroup(this.fromData.id,{name: this.fromData.name,tenantId:this.formInline.gongsiSel}).then((res)=>{
          this.dialogVisible = false;
          this.initData();
          this.$message({
            type: 'success',
            message: '编辑成功!'
          })
        })
      }else{
        this.$refs[formName].validate((valid) => {
          if (valid) {
            postDeviceGroup({name: this.fromData.name,tenantId:this.formInline.gongsiSel}).then((res)=>{
              this.dialogVisible = false
              this.initData();
              this.$message({
                type: 'success',
                message: '创建成功!'
              })
            })
          }else{
            return
          }
        })
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
